package com.wolfalpha.utron.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.bean.UActivity;
import com.wolfalpha.utron.service.ActivityService;
import com.wolfalpha.utron.service.impl.ActivityServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.StringUtils;
import com.wolfalpha.utron.utils.ThreadPoolUtils;

import easemob.chatuidemo.activity.ShowBigImageActivity;

/**
 * 活动详情
 * <p/>
 * Created by luodon on 15/9/5.
 */
public class UtronActivityDetailActivity extends BaseNormalActivity {

    private int actId = 0;                  //当前活动ID
    private ActivityService activityService;

    private UActivity uActivity;

    private TextView tvTitle;               //标题
    private TextView tvPtime;               //发布时间
    private TextView tvState;               //活动状态
    private TextView tvIntro;               //活动介绍
    private TextView tvOrganizer;           //主办方
    private TextView tvOrganizerX;          //协办方
    private TextView tvCoperation;          //赞助商
    private TextView tvAtime;               //活动时间
    private TextView tvAddress;             //活动地点
    private TextView tvPrice;               //活动价格
    private TextView tvSupplyWay;           //报名方式
    private TextView tvCustom;              //嘉宾介绍
    private ImageView ivPic;                //照片

    private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("actId")) {
            finish();
        }
        actId = bundle.getInt("actId");

        setContentView(R.layout.activity_utron_activity_detail);

        activityService = new ActivityServiceImpl();

        initView();
        initListeners();

        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        pd.show();
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                uActivity = activityService.getDetail(actId);
                if (uActivity != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvTitle.setText("【" + uActivity.getActivity_type() + "】" + uActivity.getTitle());
                            tvPtime.setText("发布时间:  " + StringUtils.friendly_time(StringUtils.TimeStamp2Date(uActivity.getTime() * 1000)));
                            int state = uActivity.getActivity_state();
                            tvState.setText(state == 0 ? "报名中" : (state == 1 ? "活动举行中" : "活动已结束"));
                            tvIntro.setText(Html.fromHtml(uActivity.getIntro()));
                            tvOrganizer.setText("主办方:  " + uActivity.getOrganizer());
                            tvOrganizerX.setText("协办方:  " + uActivity.getOrganizer_x());
                            tvCoperation.setText("合作商:  " + uActivity.getCorperation());
                            tvAtime.setText("活动时间:  " + uActivity.getActivity_time());
                            tvPrice.setText("活动价格:  " + uActivity.getPrice());
                            tvSupplyWay.setText("报名时间:  " + uActivity.getReply_way());
                            tvAddress.setText("活动地点:  " + uActivity.getActivity_address());
                            tvCustom.setText(Html.fromHtml(uActivity.getCustom_intro()));
                            Picasso.with(UtronActivityDetailActivity.this)
                                    .load(uActivity.getPic())
                                    .placeholder(R.mipmap.project_default)
                                    .into(ivPic);
                            pd.dismiss();
                        }
                    });
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            UIHelper.ToastMessage(UtronActivityDetailActivity.this, "加载失败");
                            pd.dismiss();
                        }
                    });
                }
            }
        });
    }

    private void initListeners() {
        ivPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("remotepath", uActivity.getPic());
                UIHelper.showActivity(UtronActivityDetailActivity.this, ShowBigImageActivity.class, bundle);
            }
        });
    }

    /**
     * 初始化界面
     */
    private void initView() {
        setTvTitle("优创会活动");

        tvTitle = (TextView) findViewById(R.id.tv_utron_activity_detail_title);
        tvPtime = (TextView) findViewById(R.id.tv_utron_activity_detail_ptime);
        tvState = (TextView) findViewById(R.id.tv_utron_activity_detail_state);
        tvIntro = (TextView) findViewById(R.id.tv_utron_activity_detail_intro);
        tvOrganizer = (TextView) findViewById(R.id.tv_utron_activity_detail_organizer);
        tvOrganizerX = (TextView) findViewById(R.id.tv_utron_activity_detail_organizer_x);
        tvCoperation = (TextView) findViewById(R.id.tv_utron_activity_detail_coperation);
        tvAtime = (TextView) findViewById(R.id.tv_utron_activity_detail_atime);
        tvAddress = (TextView) findViewById(R.id.tv_utron_activity_detail_address);
        tvPrice = (TextView) findViewById(R.id.tv_utron_activity_detail_price);
        tvSupplyWay = (TextView) findViewById(R.id.tv_utron_activity_detail_way);
        tvCustom = (TextView) findViewById(R.id.tv_utron_activity_detail_custom);
        ivPic = (ImageView) findViewById(R.id.iv_utron_activity_detail_pic);

        pd = new ProgressDialog(this);
        pd.setMessage("正在加载数据...");
    }


}
